#include "sprite.h"

CSprite::CSprite(CTexture *pTexture) :
    m_pTexture(pTexture),
    m_fX(0.0f),
    m_fY(0.0f),
    m_fRotation(0.0f),
    m_pSprite(nullptr),
    m_bVisible(true),
    m_fScaleX(0.62f),
    m_fScaleY(0.67f)
{
    /**
     * Constructor, ensure that we save the texture's instance and intiialise
     * the sprite.
     *
     * @param CTexture *pTexture The texture to use.
     */
    D3DXCreateSprite(CDeviceManager::getInstance()->getDevice(), &m_pSprite);
}

CSprite::~CSprite()
{
    /**
     * Release the sprite.
     */
    m_pSprite->Release();
}

void CSprite::setPosition(float fX, float fY)
{
    /**
     * Sets the position of the texture to a new position.
     *
     * @param float fX The X position of the sprite.
     * @param float fY The Y position of the sprite.
     */
    m_fX = fX;
    m_fY = fY;
}

void CSprite::getPosition(float &fX, float &fY) const
{
    /**
     * @param flaot &fX The varaible to store the X position of the sprite
     * @param flaot &fY The varaible to store the Y position of the sprite
     */
    fX = m_fX;
    fY = m_fY;
}

float CSprite::getX() const
{
    /**
     * @return float The X position of the sprite
     */
    return m_fX;
}

float CSprite::getY() const
{
    /**
     * @return float The Y position of the sprite
     */
    return m_fY;
}

void CSprite::setRotation(float fAngle)
{
    /**
     * @param float fAngle The angle rotation of the sprite (in radians)
     */
    m_fRotation = fAngle;
}

void CSprite::setRotationDegrees(float fAngle)
{
    /**
     * @param float fAngle The angle rotation of the sprite (in degrees)
     */
    m_fRotation = fAngle / 180.0f * static_cast<float>(Constants::PI);
}

float CSprite::getRotation() const
{
    /**
     * @return float The angle rotation of the sprite (in radians)
     */
    return m_fRotation;
}

float CSprite::getRotationDegrees() const
{
    /**
     * @return float The angle rotation of the sprite (in degrees)
     */
    return m_fRotation / static_cast<float>(Constants::PI) * 180.0f;
}

void CSprite::render()
{
    /**
     * Draws the sprite on the screen.
     */
    if (isVisible())
    {
        m_pSprite->Begin(D3DXSPRITE_ALPHABLEND);
        D3DXMATRIX matTransformation;

        D3DXVECTOR3 vecPosition(m_fX, m_fY, 0.0f);

        D3DXVECTOR2 vecScale = D3DXVECTOR2(getScaleX(), getScaleY());
        D3DXVECTOR2 vecCenter = D3DXVECTOR2(getX(), getY());
        D3DXVECTOR3 vecCenter3 = D3DXVECTOR3(vecCenter.x, vecCenter.y, 0.0);

        D3DXMatrixTransformation2D(&matTransformation, &vecCenter, 0.0, 
            &vecScale, &vecCenter, m_fRotation, NULL);
        m_pSprite->SetTransform(&matTransformation);

        m_pSprite->Draw(m_pTexture->getTexture(), nullptr, 
                        &vecCenter3,  &vecPosition, 0xFFFFFFFF);

        m_pSprite->End();
    }
}

void CSprite::setVisible(bool bVisible)
{
    m_bVisible = bVisible;
}

bool CSprite::isVisible() const
{
    return m_bVisible;
}

void CSprite::setScale(float fScaleX, float fScaleY)
{
    m_fScaleX = fScaleX * 0.62f;
    m_fScaleY = fScaleY * 0.68f;
}

float CSprite::getScaleX() const
{
    return m_fScaleX;
}

float CSprite::getScaleY() const
{
    return m_fScaleY;
}

LPD3DXSPRITE CSprite::getSprite() const
{
    /**
     * @return The DirectX Sprite object
     */
    return m_pSprite;
}